﻿-- Function: tepuy.classbyfield(character varying, character varying, character varying, char)

--DROP FUNCTION tepuy.classbyfield(character varying, character varying, character varying, char);

CREATE OR REPLACE FUNCTION tepuy.classbyfield(character varying, character varying, character varying, char)
  RETURNS boolean AS
--  RETURNS SETOF tepuy.base_classifier AS
$BODY$
-- Copyright © 2010 Riztan Gutierrez <riztan at gmail dot com>
-- Esto es parte de: proyecto tepuy.
-- Este programa es Software Libre conforme a la GPL Version 3 o posterior.
-- http://www.gnu.org/licenses/
DECLARE
    schematable varchar;
    tablename   varchar;
    fieldname   varchar;
    valor       char;
    query       varchar;
    registro    tepuy.base_classifier%rowtype;

BEGIN

--select flds_ref_table_name, flds_ref_field_link, class_id 
--   from ventas.base_fields
--   inner join tepuy.base_classifier on flds_ref_field_link=class_name     
--     where flds_table_name='doc_facven'
--     and flds_name='facven_estatus'

   schematable := $1 || '.' || 'base_fields ';
   tablename   := $2;
   fieldname   := $3;
   valor       := $4;

   query := 'select class_id, flds_ref_table_name, flds_ref_field_link' ||
                ' from ' || schematable  ||
                ' inner join tepuy.base_classifier on flds_ref_field_link=class_name ' 
          || ' where flds_table_name=' || quote_literal(tablename) 
          || ' and flds_name=' || quote_literal(fieldname) ;

  execute query INTO registro;

  return tepuy.statusbydoc(registro.class_name,$4,registro.class_id);

  --return next r;
   
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION tepuy.classbyfield(character varying, character varying, character varying, char) OWNER TO postgres;
COMMENT ON FUNCTION tepuy.classbyfield(character varying, character varying, character varying, char) 
   IS 'retorna verdadero si el valor para estatus del documento es correcto';
